package org.bpm.view
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import org.bpm.interfaces.IMapElement;
	
	/**
	 * This class lays out all elements of a node view.
	 * 
	 * 
	 * IMPORTANT:
	 * 
	 * To create a new node symbol in ui.fla, please follow these steps:
	 * 1,create background and name it 'bg', it's better enable 9 scale grid to the background.
	 * 2,create icon movieclip and name it icon',
	 * 3,create a textfield and name it 'labelField', icon and labelField will automaticly centered in the verticle direction,
	 * in the horizontal direction, they will lay in a line with HORIZONTAL_GAP as the gap.
	 * 4, name the symbol as same as the processdefinition.xml, but, replace all "-" to "_";
	 * 
	 * 5, further definition please check class org.bpm.models.MapNodeModel : type.
	 * */
	public class NodeMovieClip extends MovieClip implements IMapElement
	{
		protected var mc:MovieClip;
		protected var bg:MovieClip;
		protected var icon:MovieClip;
		protected var labelField:TextField;
		
		protected const HORIZONTAL_GAP:Number = 5;
		
		public function NodeMovieClip(pMc:MovieClip)
		{
			super();
			mc = pMc;
			addChild(mc);
			
			
			bg = mc.getChildByName("bg") as MovieClip;
			icon = mc.getChildByName("icon") as MovieClip;
			labelField = mc.getChildByName("labelField") as TextField;
			
			
			addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
		}
		protected function onRemoveFromStage(pEvent:Event):void{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			
			bg = null;
			icon = null;
			labelField = null;
			
			
			if(mc){
				removeChild(mc);
				mc = null;
			}
		}
		override public function set width(value:Number):void{
			if(bg){
				bg.width = value;
			}else{
				super.width = value;
			}
		}
		override public function get width():Number{
			if(bg){
				return bg.width;
			}
			return super.width;
		}
		
		override public function set height(value:Number):void{
			if(bg){
				bg.height = value;
			}else{
				super.height = value;
			}
		}
		override public function get height():Number{
			if(bg){
				return bg.height;
			}
			return super.height;
		}
		
		public function highlight():void{
			if(bg){
				try{
					bg.gotoAndStop("DONE");
				}catch(e:Error){
					/***/
				}
			}
		}
		public function blur():void{
			if(bg) {
				try{
					bg.gotoAndStop("UNDONE");
				}catch(e:Error){
					/***/
				}
			}
		}
		public function updateLabel(pValue:String):void{
			if(labelField){
				labelField.text = pValue;
				labelField.autoSize = TextFieldAutoSize.LEFT;
				labelField.width = labelField.textWidth + 20;
			}
			updateView();
		}
		
		
		protected function updateView():void{
			if(bg) {
				bg.filters = [new DropShadowFilter(2,45,0,.2,7,7,1,3)];
			}
			
			
			if(icon){
				icon.y = 0;
				if(labelField){
					icon.x = -(icon.width + HORIZONTAL_GAP + labelField.textWidth +20)/2 + icon.width/2;
					labelField.x = icon.x + icon.width/2 + HORIZONTAL_GAP;
				}else{
					icon.x = 0;
				}
			}else{
				if(labelField){
					labelField.x = -labelField.textWidth/2;
				}
			}
		}
	}
}